/**
 * @a https://leetcode.cn/problems/contains-duplicate-ii/
 */

#include "common.h"

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        if(k == 0) return false;
        unordered_map<int,int> num_index;
        int i = 0;
        for(auto num:nums){
            if(num_index.count(num) && i - num_index[num] <= k){
                return true;
            }
            num_index[num] = i;
            ++i;
        }
        return false;
    }
};